﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class ContractDelivery: IContractDelivery
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public ContractDelivery(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.ContractDelivery GetContractDelivery(System.Int32 contractDeliveryID)
		{
			BEGDC.PreContract.DAL.ContractDelivery contractDelivery = BEGDC.PreContract.DAL.ContractDelivery.TryFind(contractDeliveryID);

            return contractDelivery;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetContractDelivery_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM ContractDelivery";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteContractDelivery(System.Int32 contractDeliveryID)
		{
			try
            {
				BEGDC.PreContract.DAL.ContractDelivery contractDelivery = BEGDC.PreContract.DAL.ContractDelivery.TryFind(contractDeliveryID);
                contractDelivery.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractDelivery", string.Format("Error occured when deleting the ContractDelivery, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddContractDelivery(BEGDC.PreContract.DAL.PreContractInfo preContractID, Nullables.NullableDecimal percentage, Nullables.NullableDateTime updateDate, System.String comments)	
		{
			try
            {
				BEGDC.PreContract.DAL.ContractDelivery contractDelivery = new BEGDC.PreContract.DAL.ContractDelivery();
				
				contractDelivery.PreContractID = preContractID;
				contractDelivery.Percentage = percentage;
				contractDelivery.UpdateDate = updateDate;
				contractDelivery.Comments = comments;
				contractDelivery.CreateAndFlush();
				
				return contractDelivery.ContractDeliveryID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractDelivery", string.Format("Error occured when adding a new ContractDelivery, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateContractDelivery(System.Int32 contractDeliveryID, BEGDC.PreContract.DAL.PreContractInfo preContractID, Nullables.NullableDecimal percentage, Nullables.NullableDateTime updateDate, System.String comments)
		{
			try
            {
                BEGDC.PreContract.DAL.ContractDelivery contractDelivery = GetContractDelivery(contractDeliveryID);

				contractDelivery.PreContractID = preContractID;
				contractDelivery.Percentage = percentage;
				contractDelivery.UpdateDate = updateDate;
				contractDelivery.Comments = comments;
				contractDelivery.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ContractDelivery", string.Format("Error occured when updating the ContractDelivery, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



